﻿#region Using Directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Controls;

using CSLive.LiveMessenger;

using Control=CSLive.ControlPanel.Editor.Control;

#endregion

namespace CSLive.Controls
{
    [ToolboxData( "<{0}:Presence runat=server></{0}:PresenceLink>" )]
    public class Presence : ObjectDataBase, ICSLiveControl
    {
        private User _user;

        #region Methods

        public override object DataSource
        {
            get
            {
                if ( _user == null )
                {
                    return _user = CSControlUtility.Instance().GetCurrentUser( this );
                }

                return _user;
            }
            set
            {
                if ( value is User )
                    _user = value as User;
                else
                    throw new InvalidCastException( "DataSource must be a valid User object" );
            }
        }

        protected override HyperLink GetHyperLink()
        {
            var user = DataSource as User;
            if ( user == null )
                return null;

            CSContext csContext = CSControlUtility.Instance().GetCurrentCSContext( Page );

            var link = new HyperLink();
            if ( string.IsNullOrEmpty( user.GetExtendedAttribute( "LivePresence" ) ) || csContext.User.IsAnonymous )
                AutomatedVisible = false;
            else
            {
                if ( ( ( PresenceLinkTo ) LiveConfiguration.Instance.getConfigSetting( Name, "Link To" ).Value ) == PresenceLinkTo.MessengerWindow )
                {
                    link.NavigateUrl = "http://settings.messenger.live.com/conversation/imme.aspx?invitee=" + user.GetExtendedAttribute( "LivePresence" ) + "&mkt=" + user.Profile.Language;
                    link.Attributes.Add( "onclick", "window.open(this.href, '" + LinkTarget + "', 'height=400,width=300,status=no,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes,directories=no');" );
                }
                else link.NavigateUrl = "javascript:void(0);";
                Text = UseImage ? string.Format(
                                      "<img src=\"http://messenger.services.live.com/users/{0}/presenceimage\" alt=\"{1}\" />",
                                      user.GetExtendedAttribute( "LivePresence" ),
                                      string.Format( FormatString, user.DisplayName ) ) : string.Format( FormatString, user.DisplayName );
            }
            return link;
        }

        #endregion

        #region Properties

        public Dictionary< string, Control > EditorControls
        {
            get
            {
                Dictionary< string, Control > dictionary;
                dictionary = new Dictionary< string, Control >
                                 {
                                     {
                                         "Presence Text Link", new Control( "Presence Text",
                                                                            "Windows Live Messenger Presence Text\nDisplays the users display name, and allows linking to a Windows Live Web Messenger Window",
                                                                            true,
                                                                            "<CSLive:Presence UseImage=\"false\" runat=\"server\" />" )
                                         },
                                     {
                                         "Presence Image Link", new Control( "Presence Image",
                                                                             "Windows Live Messenger Presence Image.\nDisplays the messenger man symbolising the user's status and allows linking to a Windows Live Web Messenger Window",
                                                                             true,
                                                                             "<CSLive:Presence UseImage=\"true\" runat=\"server\" />" )
                                         }
                                 };
                return dictionary;
            }
        }

        public string Name
        {
            get { return "Live Presence Link Control"; }
        }

        public Uri DocsUri
        {
            get
            {
                return
                    new Uri(
                        "http://www.codeplex.com/CSLive/Wiki/View.aspx?title=Live%20Presence&referringTitle=CSLive%202.0" );
            }
        }

        public string Description
        {
            get { return "Windows Live IM Presence Link Control"; }
        }

        public string Group
        {
            get { return "Live Messenger"; }
        }

        #endregion

        [Bindable( true )]
        [DefaultValue( "false" )]
        [Localizable( true )]
        public bool UseImage
        {
            get { return ( ViewState[ "UseImage" ] == null ) ? false : ( bool ) ViewState[ "UseImage" ]; }
            set { ViewState[ "UseImage" ] = value; }
        }
    }
}